<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>我的预约 - 宠物医院管理系统</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/css/style.css" rel="stylesheet">
    <style>
        .appointment-card {
            transition: all 0.3s ease;
            border: none;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            margin-bottom: 20px;
        }
        .appointment-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 20px rgba(0,0,0,0.15);
        }
        .status-badge {
            position: absolute;
            top: 15px;
            right: 15px;
            font-size: 0.8rem;
            padding: 4px 8px;
        }
        .appointment-time {
            font-size: 1.2rem;
            font-weight: 600;
            color: #495057;
        }
        .search-form {
            background: white;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            margin-bottom: 20px;
        }
    </style>
</head>
<body>
    <div class="container-fluid">
        <div class="row">
            <!-- 侧边栏 -->
            <div class="col-md-2 p-0">
                <%@ include file="../common/user-sidebar.jsp" %>
            </div>
            
            <!-- 主内容区 -->
            <div class="col-md-10">
                <div class="main-content p-4">
                    <!-- 页面标题 -->
                    <div class="d-flex justify-content-between align-items-center mb-4">
                        <h2><i class="fas fa-calendar-check me-2"></i>我的预约</h2>
                        <nav aria-label="breadcrumb">
                            <ol class="breadcrumb">
                                <li class="breadcrumb-item"><a href="${pageContext.request.contextPath}/pages/user/dashboard.jsp">首页</a></li>
                                <li class="breadcrumb-item active">我的预约</li>
                            </ol>
                        </nav>
                    </div>

                    <!-- 提示信息 -->
                    <c:if test="${not empty param.success}">
                        <div class="alert alert-success alert-dismissible fade show" role="alert">
                            <i class="fas fa-check-circle me-2"></i>${param.success}
                            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                        </div>
                    </c:if>
                    <c:if test="${not empty param.error}">
                        <div class="alert alert-danger alert-dismissible fade show" role="alert">
                            <i class="fas fa-exclamation-circle me-2"></i>${param.error}
                            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                        </div>
                    </c:if>

                    <!-- 搜索表单 -->
                    <div class="search-form">
                        <form method="get" action="${pageContext.request.contextPath}/appointment/my-appointments">
                            <input type="hidden" name="action" value="my-appointments">
                            <div class="row g-3">
                                <div class="col-md-3">
                                    <label class="form-label">宠物姓名</label>
                                    <input type="text" class="form-control" name="petName" value="${petName}" placeholder="请输入宠物姓名">
                                </div>
                                <div class="col-md-3">
                                    <label class="form-label">服务类型</label>
                                    <select class="form-select" name="serviceType">
                                        <option value="">全部类型</option>
                                        <c:forEach var="type" items="${serviceTypes}">
                                            <option value="${type}" ${serviceType == type ? 'selected' : ''}>${type}</option>
                                        </c:forEach>
                                    </select>
                                </div>
                                <div class="col-md-3">
                                    <label class="form-label">预约状态</label>
                                    <select class="form-select" name="status">
                                        <option value="">全部状态</option>
                                        <option value="pending" ${status == 'pending' ? 'selected' : ''}>待确认</option>
                                        <option value="confirmed" ${status == 'confirmed' ? 'selected' : ''}>已确认</option>
                                        <option value="completed" ${status == 'completed' ? 'selected' : ''}>已完成</option>
                                        <option value="cancelled" ${status == 'cancelled' ? 'selected' : ''}>已取消</option>
                                    </select>
                                </div>
                                <div class="col-md-3">
                                    <label class="form-label">预约日期</label>
                                    <input type="date" class="form-control" name="appointmentDate" value="${appointmentDate}">
                                </div>
                            </div>
                            <div class="row mt-3">
                                <div class="col-12">
                                    <button type="submit" class="btn btn-primary">
                                        <i class="fas fa-search me-2"></i>搜索
                                    </button>
                                    <a href="${pageContext.request.contextPath}/appointment/my-appointments?action=my-appointments" class="btn btn-outline-secondary ms-2">
                                        <i class="fas fa-undo me-2"></i>重置
                                    </a>
                                    <a href="${pageContext.request.contextPath}/pages/user/appointment-add.jsp" class="btn btn-success ms-2">
                                        <i class="fas fa-plus me-2"></i>新建预约
                                    </a>
                                </div>
                            </div>
                        </form>
                    </div>

                    <!-- 预约列表 -->
                    <c:choose>
                        <c:when test="${empty pageResult.data}">
                            <div class="text-center py-5">
                                <i class="fas fa-calendar-times fa-5x text-muted mb-4"></i>
                                <h4 class="text-muted">还没有预约记录</h4>
                                <p class="text-muted">点击下方按钮为您的宠物预约服务吧</p>
                                <a href="${pageContext.request.contextPath}/pages/user/appointment-add.jsp" class="btn btn-primary btn-lg">
                                    <i class="fas fa-plus me-2"></i>立即预约
                                </a>
                            </div>
                        </c:when>
                        <c:otherwise>
                            <div class="row">
                                <c:forEach var="appointment" items="${pageResult.data}">
                                    <div class="col-md-6 col-lg-4 mb-4">
                                        <div class="card appointment-card h-100">
                                            <div class="position-relative">
                                                <span class="badge status-badge 
                                                    ${appointment.status == 'pending' ? 'bg-warning' : 
                                                      appointment.status == 'confirmed' ? 'bg-info' : 
                                                      appointment.status == 'completed' ? 'bg-success' : 'bg-secondary'}">
                                                    ${appointment.statusDisplay}
                                                </span>
                                                <div class="card-body">
                                                    <h5 class="card-title">
                                                        <i class="fas fa-calendar-alt me-2"></i>
                                                        预约 #${appointment.appointmentId}
                                                    </h5>
                                                    
                                                    <div class="mb-3">
                                                        <div class="appointment-time">
                                                            <fmt:formatDate value="${appointment.appointmentDate}" pattern="yyyy年MM月dd日"/>
                                                        </div>
                                                        <small class="text-muted">
                                                            <i class="fas fa-clock me-1"></i>
                                                            <fmt:formatDate value="${appointment.appointmentTime}" pattern="HH:mm"/>
                                                        </small>
                                                    </div>
                                                    
                                                    <div class="row mb-3">
                                                        <div class="col-6">
                                                            <small class="text-muted">宠物</small>
                                                            <div class="fw-bold">
                                                                <c:choose>
                                                                    <c:when test="${not empty appointment.pet}">
                                                                        ${appointment.pet.petName}
                                                                    </c:when>
                                                                    <c:otherwise>未知</c:otherwise>
                                                                </c:choose>
                                                            </div>
                                                        </div>
                                                        <div class="col-6">
                                                            <small class="text-muted">服务类型</small>
                                                            <div class="fw-bold">${appointment.serviceType}</div>
                                                        </div>
                                                    </div>
                                                    
                                                    <c:if test="${not empty appointment.doctorName}">
                                                        <div class="mb-3">
                                                            <small class="text-muted">医生</small>
                                                            <div class="fw-bold">
                                                                <i class="fas fa-user-md me-1"></i>${appointment.doctorName}
                                                            </div>
                                                        </div>
                                                    </c:if>
                                                    
                                                    <c:if test="${not empty appointment.symptoms}">
                                                        <div class="mb-3">
                                                            <small class="text-muted">症状描述</small>
                                                            <div class="text-truncate" title="${appointment.symptoms}">
                                                                ${appointment.symptoms}
                                                            </div>
                                                        </div>
                                                    </c:if>
                                                    
                                                    <div class="d-flex justify-content-between align-items-center">
                                                        <small class="text-muted">
                                                            创建时间：<fmt:formatDate value="${appointment.createTime}" pattern="MM-dd HH:mm"/>
                                                        </small>
                                                    </div>
                                                </div>
                                                
                                                <div class="card-footer bg-transparent">
                                                    <div class="btn-group w-100">
                                                        <a href="${pageContext.request.contextPath}/appointment/view?action=view&id=${appointment.appointmentId}" 
                                                           class="btn btn-outline-info btn-sm">
                                                            <i class="fas fa-eye"></i> 查看
                                                        </a>
                                                        <c:if test="${appointment.status == 'pending' || appointment.status == 'confirmed'}">
                                                            <a href="${pageContext.request.contextPath}/appointment/edit?action=edit&id=${appointment.appointmentId}" 
                                                               class="btn btn-outline-primary btn-sm">
                                                                <i class="fas fa-edit"></i> 编辑
                                                            </a>
                                                        </c:if>
                                                        <c:if test="${appointment.status == 'pending'}">
                                                            <button type="button" class="btn btn-outline-warning btn-sm" 
                                                                    onclick="cancelAppointment(${appointment.appointmentId})">
                                                                <i class="fas fa-times"></i> 取消
                                                            </button>
                                                        </c:if>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </c:forEach>
                            </div>
                        </c:otherwise>
                    </c:choose>

                    <!-- 分页 -->
                    <c:if test="${pageResult.totalPages > 1}">
                        <nav class="mt-4">
                            <ul class="pagination justify-content-center">
                                <li class="page-item ${pageResult.hasPrevious ? '' : 'disabled'}">
                                    <a class="page-link" href="?action=my-appointments&page=${pageResult.currentPage - 1}&petName=${petName}&serviceType=${serviceType}&status=${status}&appointmentDate=${appointmentDate}">
                                        <i class="fas fa-chevron-left"></i>
                                    </a>
                                </li>
                                
                                <c:forEach begin="1" end="${pageResult.totalPages}" var="i">
                                    <c:if test="${i <= 5 || i > pageResult.totalPages - 5 || (i >= pageResult.currentPage - 2 && i <= pageResult.currentPage + 2)}">
                                        <li class="page-item ${i == pageResult.currentPage ? 'active' : ''}">
                                            <a class="page-link" href="?action=my-appointments&page=${i}&petName=${petName}&serviceType=${serviceType}&status=${status}&appointmentDate=${appointmentDate}">${i}</a>
                                        </li>
                                    </c:if>
                                </c:forEach>
                                
                                <li class="page-item ${pageResult.hasNext ? '' : 'disabled'}">
                                    <a class="page-link" href="?action=my-appointments&page=${pageResult.currentPage + 1}&petName=${petName}&serviceType=${serviceType}&status=${status}&appointmentDate=${appointmentDate}">
                                        <i class="fas fa-chevron-right"></i>
                                    </a>
                                </li>
                            </ul>
                            <div class="text-center text-muted">
                                ${pageResult.pageInfo}
                            </div>
                        </nav>
                    </c:if>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        // 取消预约
        function cancelAppointment(appointmentId) {
            const reason = prompt('请输入取消原因（可选）：');
            if (reason !== null) { // 用户点击了确定（包括空字符串）
                fetch('${pageContext.request.contextPath}/appointment/cancel', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded',
                    },
                    body: 'action=cancel&id=' + appointmentId + '&reason=' + encodeURIComponent(reason || '')
                })
                .then(response => response.text())
                .then(data => {
                    if (data === 'success') {
                        location.reload();
                    } else {
                        alert('取消失败');
                    }
                });
            }
        }
    </script>
</body>
</html>
